package com.demo.config.apache.temporary;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TitleExtractor {

	static String fileName = "D:\\file\\pdf\\testPdf.pdf";

	public static void main(String[] args) throws IOException {
		// PDF 文档

		File pdfFile = new File(fileName);
		List<String> pdfTitles = extractTitlesFromPdf(pdfFile);
		System.out.println("PDF 标题：");
		for (String title : pdfTitles) {
			System.out.println(title);
		}

		// Word 文档（DOC）
		// List<String> docTitles = extractTitlesFromDoc("D:\\file\\投标文件.doc");
		// System.out.println("DOC 标题：");
		// for (String title : docTitles) {
		// System.out.println(title);
		// }

		// Word 文档（DOCX）
		// File docxFile = new File("D:\\file\\投标文件.doc");
		// List<String> docxTitles = extractTitlesFromDocx(docxFile);
		// System.out.println("DOCX 标题：");
		// for (String title : docxTitles) {
		// System.out.println(title);
		// }
	}

	private static List<String> extractTitlesFromPdf(File pdfFile) throws IOException {
		// 加载 pdf 文档,获取PDDocument文档对象
		PDDocument document = Loader.loadPDF(pdfFile);
		try (document) {
			PDFTextStripper stripper = new PDFTextStripper();
			String text = stripper.getText(document);

			List<String> titles = new ArrayList<>();
			String[] lines = text.split("\n");
			for (String line : lines) {
				if (line.matches(".*\\s*(bold|B)\\s+.*")) {
					titles.add(line.trim());
				}
				else if (line.matches(".*\\s*(font-size:\\s*[1-9]\\dpx)\\s+.*")) {
					titles.add(line.trim());
				}
			}

			return titles;
		}
	}

	private static List<String> extractTitlesFromDoc(String filePath) throws IOException {
		try (HWPFDocument document = new HWPFDocument(new FileInputStream(filePath))) {
			Range range = document.getRange();
			List<String> titles = new ArrayList<>();

			for (int i = 0; i < range.numParagraphs(); i++) {
				Paragraph paragraph = range.getParagraph(i);
				System.out.println(paragraph.getStyleIndex());
				// List.of(Paragraph.);
				// if (paragraph.getStyleIndex() == Paragraph.) {
				// titles.add(paragraph.text().trim());
				// }
			}

			return titles;
		}
	}

	private static List<String> extractTitlesFromDocx(File docxFile) throws IOException {
		try (XWPFDocument document = new XWPFDocument(OPCPackage.open(docxFile))) {
			List<String> titles = new ArrayList<>();

			for (XWPFParagraph paragraph : document.getParagraphs()) {
				String style = paragraph.getStyle();
				if (style != null && style.startsWith("Heading")) {
					titles.add(paragraph.getText().trim());
				}
				else {
					// 是否加粗
					boolean isBold = false;
					for (XWPFRun run : paragraph.getRuns()) {
						if (run.isBold()) {
							isBold = true;
							break;
						}
					}
					// 字体大小
					int fontSize = -1;
					for (XWPFRun run : paragraph.getRuns()) {
						if (fontSize == -1) {
							fontSize = run.getFontSize();
						}
						else if (fontSize != run.getFontSize()) {
							fontSize = -2;
							break;
						}
					}
					if (isBold && fontSize > 12) {
						titles.add(paragraph.getText().trim());
					}

				}
			}

			return titles;
		}
		catch (InvalidFormatException e) {
			throw new RuntimeException(e);
		}
	}

}